package com.sevenGroup.pawnshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sevenGroup.pawnshop.dao.AccountDao;
import com.sevenGroup.pawnshop.dao.UsersDao;
import com.sevenGroup.pawnshop.model.Account;
import com.sevenGroup.pawnshop.model.Users;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;

@Service
public class LoginService implements UserDetailsService {
    @Resource
    private AccountDao ad;

    @Resource
    private UsersDao usersDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Account account = ad.selectOne(new QueryWrapper<Account>().eq("account_username", username).select("account_id","account_username","account_password","account_tel"));
        if (account == null) throw new UsernameNotFoundException("用户名未找到");

        Users users = usersDao.selectOne(new QueryWrapper<Users>().eq("account_id", account.getAccountId()).select("user_id","user_name","account_id","role_id","role_manage","role_name"));

        account.setRoles(Arrays.asList(users.getRoleManage()));
        return account;
    }
}
